import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import { useUserStore } from '@/stores/user'
import { storeToRefs } from 'pinia'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: HomeView,
    },
    // 新增商品详情路由
    {
      path: '/HuangDetail/:id',
      name: 'HuangDetail',
      component: () => import('../views/HuangDetail.vue'),
      props: true
    },
    {
    path: '/huangchannel',
    name: 'Huangchannel',
    component: () => import('@/views/Huangchannel.vue'),
    props: true
    } ,
        {
      path: '/HuangDetail2/:id',
      name: 'HuangDetail2',
      component: () => import('../views/HuangDetail2.vue'),
      props: true
    },
    {
      path: '/Huanglogin',
      name: 'Huanglogin',
      component: () => import('../views/Huanglogin.vue'),
    },
    {
      path: '/404',
      name: '404',
      component: () => import('../views/404.vue'),
    },{
      path: '/Huangmy',
      name: 'Huangmy',
      component: () => import('../views/Huangmy.vue'),
    }
    // ... 可继续添加其他路由以满足7个路径要求
  ],
})

router.beforeEach((to, from, next) => {
  const userStore = useUserStore()
  const whiteList = ['/Huanglogin']
  if (whiteList.includes(to.path)) {
    next()
    return
  }

  if (!userStore.token) {
    next({
      path: '/Huanglogin',
      query: {
        redirect: to.fullPath
      }
    })
  } else {
    next()
  }
})

export default router
